package wjh.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import wjh.mapper.RegisterMapper;
import wjh.pojo.Department;
import wjh.pojo.Doctor;
import wjh.pojo.Patient;
import wjh.pojo.Register;

import javax.servlet.http.HttpServletRequest;
import java.sql.Date;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

@Service
public class RegisterServiceImpl implements RegisterService{
    @Autowired
    private RegisterMapper registerMapper;

    @Override
    public List<Register> findByNum(String records_number,int patientId) {
        return registerMapper.findByNum(records_number,patientId);
    }
    @Override
    public void addRegister(Register register) {
        registerMapper.addRegister(register);
    }

    @Override
    public void deleteRegisters(String ids) {
        String[] idArray = ids.split("-");
        for (String id : idArray) {
            if (!id.isEmpty()) {
                registerMapper.deleteById(Integer.parseInt(id));
            }
        }
    }

    @Override
    public Register findRecordsById(Integer id) {
       return registerMapper.findRecordsById(id);
    }


    @Override
    public boolean signIn(Integer id) {
        // 获取记录
        Register record = registerMapper.findById(id);
        if (record == null) {
            return false; // 记录不存在
        }
        // 判断是否已签到
        if ("已签到".equals(record.getStatus())) {
            return false; // 已签到
        }
        // 获取当前日期和时间
        LocalDateTime now = LocalDateTime.now();
        java.util.Date utilDate = record.getDate(); // 获取数据库中的日期
        LocalDateTime recordDateTime = utilDate.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDateTime(); // 转换为 LocalDateTime

        // 假设签到时间为当天上午9点
        LocalDateTime signInDeadline = recordDateTime.withHour(9).withMinute(0).withSecond(0);
        LocalDateTime signInEnd = recordDateTime.withHour(18).withMinute(0).withSecond(0);

        // 判断是否未到签到时间
        if (now.isBefore(signInDeadline) || now.isAfter(signInEnd)) {
            return false; // 未到签到时间
        }


        // 判断是否是签到日期
        if (!now.toLocalDate().equals(recordDateTime.toLocalDate())) {
            return false; // 不是签到日期
        }

        // 更新签到状态
        return registerMapper.updateStatus(id, "已签到") > 0;
    }

    @Override
    public Patient findPatientById(int id) {
        return registerMapper.findPatientById(id);
    }

    @Override
    public Integer getDoctorIdByName(String doctorName) {
        return registerMapper.getDoctorIdByName(doctorName);
    }

    @Override
    public Doctor findDoctorById(int id) {
        return registerMapper.findDoctorById(id);
    }

    @Override
    public List<Register> doctorFindAll(int doctor_id, String user_name, String records_number) {
        return registerMapper.doctorFindAll(doctor_id, user_name, records_number);
    }

    @Override
    public void fillRecord(String orders,int id) {
        registerMapper.fillRecord(orders,id);
    }

    @Override
    public List<Register> findAllRecords(String user_name) {
        return registerMapper.findAllRecords(user_name);
    }

    @Override
    public int getRegistrationFee(String doctorId) {
        return registerMapper.getRegistrationFee(doctorId);
    }

    @Override
    public void addRegisterFee(String recordsNumber, Integer registerFee) {
        registerMapper.addRegisterFee(recordsNumber,registerFee);
    }

    @Override
    public void cancelRegisters(String id) {
        registerMapper.cancelRegisters(id);
    }

    @Override
    public void decrementPlanNumber(Integer doctorId, String dayOfWeek) {
        registerMapper.decrementPlanNumber(doctorId,dayOfWeek);
    }


    @Override
    public List<Department> findAllDepartments() {
        return registerMapper.findAllDepartments();
    }

    @Override
    public List<Doctor> findDoctorsByDateAndDepartment(String date, String id) {
        return registerMapper.findDoctorsByDateAndDepartment(date, id);
    }




}
